package com.jfdesgagne.traklib.ui.components.options {
	import fl.controls.Button;
	import fl.controls.Slider;

	import com.jfdesgagne.traklib.collections.TrakConfiguration;
	import com.jfdesgagne.traklib.statics.TrakTypes;
	import com.jfdesgagne.traklib.tracker.TrakColor;
	import com.jfdesgagne.traklib.ui.components.AbstractTrakUIOptions;
	import com.jfdesgagne.traklib.ui.events.TrakUIEvent;

	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author jdesgagne
	 */
	public class TrakUIColorOptions extends AbstractTrakUIOptions {
		public var colorSensitivity:Slider;
		public var changeColor:Button;		public var addColor:Button;

		public function TrakUIColorOptions():void {
			NAME = TrakTypes.COLOR_TRACKING;
			this.addEvents();
		}

		override public function addEvents():void {
			super.addEvents();
			this.colorSensitivity.addEventListener(Event.CHANGE, this.setValues);			this.changeColor.addEventListener(MouseEvent.CLICK, this.callTrackerFunction);			this.addColor.addEventListener(MouseEvent.CLICK, this.callTrackerFunction);
		}

		override public function removeEvents():void {
			super.removeEvents();
			this.colorSensitivity.removeEventListener(Event.CHANGE, this.setValues);
			this.changeColor.removeEventListener(MouseEvent.CLICK, this.callTrackerFunction);
			this.addColor.removeEventListener(MouseEvent.CLICK, this.callTrackerFunction);
		}

		protected function callTrackerFunction(e:MouseEvent):void {
			var functionName:String;
			if(e.currentTarget == this.changeColor) {
				functionName = TrakColor.CALL_CHANGE_COLOR;
			} else if(e.currentTarget == this.addColor) {
				functionName = TrakColor.CALL_ADD_COLOR;
			}
			
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.CALL_FUNCTION_ON_TRACKER, functionName, 0, true, true));
		}

		override protected function setValues(e:Event):void {
			var valueType:String, value:int;
			switch( e.currentTarget ) {
				case this.colorSensitivity:
					valueType = TrakConfiguration.COLOR_SENSITIVITY;
					value = this.colorSensitivity.value;
					break;
				default:
					super.setValues(e);
			}
			this.stage.focus = this.stage;
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, valueType, value, true, true));
		}

		override public function setCalibration():void {
			super.setCalibration();
			
			this.colorSensitivity.value = TrakConfiguration.colorSensitivity;
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, TrakConfiguration.COLOR_SENSITIVITY, this.colorSensitivity.value, true, true));
		}
	}
}
